package vip.websky.project.user.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.SysUserBO;
import vip.websky.project.module.entity.user.SysUser;
import vip.websky.project.user.dao.UserDao;

import java.util.List;

/**
 * 系统用户 服务类
 *
 * @Author Yong.Yang E-mail:yang2048@qq.com
 * @Create 2017/8/13-16:10
 */

/**
 * <ul>
 * <li>1.创建用户</li>
 * <li>2.查询用户</li>
 * <li>3.查询用户列表</li>
 * </ul>
 */
@Slf4j
@Service
public class UserServer extends ServiceImpl<UserDao, SysUser> {

    @Autowired
    private UserDao userDao;

    /**
     * 1.创建用户
     *
     * @param sysUserBO 用户信息
     * @return SysUserBO
     */
    @KingLog
    public SysUserBO insertUser(SysUserBO sysUserBO) {
        SysUser sysUser = ObjectConvertUtils.copyToDest(sysUserBO, SysUser.class);
        Integer result = userDao.insert(sysUser);
        return result > 0 ? ObjectConvertUtils.copyToDest(sysUser, SysUserBO.class) : null;
    }

    /**
     * 根据条件查找一个用户
     *
     * @param sysUserBO 唯有条件
     * @return SysUserBO
     */
    @KingLog
    public SysUserBO selectUser(SysUserBO sysUserBO) {
        EntityWrapper<SysUserBO> ew = new EntityWrapper<>();
        ew.where("u.USER_STATUS != {0}", -1)
                .groupBy("u.id");
        if (StrUtil.isNotBlank(String.valueOf(sysUserBO.getId())) && sysUserBO.getId()!=null)
            ew.andNew().where("u.ID = {0}", sysUserBO.getId());
        if (StrUtil.isNotBlank(sysUserBO.getUserName()))
            ew.andNew().where("u.USER_NAME = {0}", sysUserBO.getUserName());
        /*if (StrUtil.isNotBlank(sysUserBO.getPassword()))
            ew.andNew().where("u.`PASSWORD` = {0}", sysUserBO.getPassword());*/
        return userDao.selectUser(ew);
    }

    /**
     * 获取用户列表
     *
     * @param sysUserBO 用户信息
     * @return Page<SysUserBO>
     */
    @KingLog
    public Page<SysUserBO> selectUserList(SysUserBO sysUserBO) {
        Page<SysUserBO> page = new Page<>(sysUserBO.getCurPage(), sysUserBO.getPageSize());
        EntityWrapper<SysUserBO> ew = new EntityWrapper<>();
        ew.where("u.USER_STATUS != {0}", "-1")
                .orderBy("userStatus", false)
                .orderBy("id", false)
                .groupBy("u.id");
        //TODO 查询条件
        //if (StringUtils.isNotBlank(sysUserBO.getUserName())) ew.andNew().or("`USERNAME` = {0}", sysUserBO.getUserName());
        List<SysUserBO> sysUserBOList = userDao.selectUser(page, ew);
        page.setRecords(sysUserBOList);
        return page;
    }

    /**
     * 修改用户信息
     *
     * @param sysUserBO 用户信息
     * @return SysUserBO
     */
    @KingLog
    public SysUserBO updateUser(SysUserBO sysUserBO) {
        SysUser sysUser = ObjectConvertUtils.copyToDest(sysUserBO, SysUser.class);
        Integer result = userDao.updateById(sysUser);
        return result > 0 ? ObjectConvertUtils.copyToDest(sysUser, SysUserBO.class) : null;
    }

    /**
     * 删除用户
     *
     * @param sysUserBO 用户信息
     * @return boolean
     */
    @KingLog
    public boolean delUser(SysUserBO sysUserBO) {
        SysUser sysUser = ObjectConvertUtils.copyToDest(sysUserBO, SysUser.class);
        Integer result = userDao.deleteById(sysUser.getId());
        return result > 0;
    }

}
